package me.imli.app.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import me.imli.app.dao.RssDataHelper;
import android.database.Cursor;

import com.google.gson.Gson;

public class RssFeedModel extends BaseModel implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8912168384358838678L;

    private static final HashMap<String, RssFeedModel> CACHE = new HashMap<String, RssFeedModel>();
	
	/** 标题 */
	public String title;
	/** 描述 */
	public String description;
	/** 链接 */
	public String link;
	/** 最后编辑时间 */
	public String lastBuildDate;
	/** 发布时间 */
	public String pubDate;
	/**Time To Live */
	public String ttl;
	
	/** Item集合 */
	public ArrayList<RssItemModel> items;
	
	public RssFeedModel() {
		items = new ArrayList<RssItemModel>();
	}
	

    private static void addToCache(RssFeedModel model) {
        CACHE.put(model.link, model);
    }
    
    private static RssFeedModel getFromCache(String id) {
        return CACHE.get(id);
    }
    
    public static RssFeedModel fromJson(String json) {
    	RssFeedModel model = new Gson().fromJson(json, RssFeedModel.class);
        return model;
    }

    public static RssFeedModel fromCursor(Cursor cursor) {
        String id = cursor.getString(cursor.getColumnIndex(RssDataHelper.RssDBInfo.ID));
        RssFeedModel model = getFromCache(id);
        if (model != null) {
            return model;
        }
        model = new Gson().fromJson(cursor.getString(cursor.getColumnIndex(RssDataHelper.RssDBInfo.JSON)), RssFeedModel.class);
        addToCache(model);
        return model;
    }
	
}
